import 'package:flutter/material.dart';

Color hexToColor(String? hex) {
  if (hex == null || hex.isEmpty) {
    return Colors.transparent;
  }
  if (!hex.startsWith('#')) {
    switch (hex) {
      case 'white':
        return Colors.white;
      case 'red':
        return Colors.red;
      case 'blue':
        return Colors.blue;
      case 'green':
        return Colors.green;
      case 'yellow':
        return Colors.yellow;
      case 'orange':
        return Colors.orange;
      case 'purple':
        return Colors.purple;
      case 'pink':
        return Colors.pink;
      case 'brown':
        return Colors.brown;
      case 'indigo':
        return Colors.indigo;
      default:
        return Colors.transparent;
    }
  }

  hex = hex.replaceFirst('#', '');

  switch (hex.length) {
    case 3:
      hex = hex[0] * 2 + hex[1] * 2 + hex[2] * 2;
      break;
    case 6:
      hex = 'FF$hex';
      break;
    case 8:
      break;
    default:
      return Colors.transparent;
  }

  return Color(int.parse(hex, radix: 16));
}
